package com.ruoyi.car.controller.admin.model;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarModelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.PageReq;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author wy
 * @Description 管理后台-汽车车型维护
 * @Date 2023/6/5 14:49
 * @Version 1.0
 */
// swagger注解
@Api(tags = "管理后台-汽车车型维护")
// springmvc的 @Controller+@ResponseBody
@RestController
// springmvc注解
@RequestMapping(value = "/car/model")
public class CarModelController {

    @Autowired
    private CarModelService carModelService;


    // spring security权限
    @PreAuthorize(value = "@ss.hasPermi('car:model:page')")
    // springmvc  get请求注解
    @GetMapping(value = "/page")
    // swagger3 的注解
   // @Operation(summary = "获取分页列表")
    @ApiOperation(value = "获取分页列表")
    public TableDataInfo  list(PageReq req){
        PageInfo<CarModel> pageInfo = this.carModelService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarModelResp> list= CarModelConvert.INSTANCE.convert(pageInfo.getList());

        TableDataInfo tableDataInfo=new TableDataInfo(list,count);
        return tableDataInfo;
    }

    @Log(title = "汽车模型",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('car:model:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R  add(@RequestBody @Valid CarModelAddReq  req){
        CarModel convert = CarModelConvert.INSTANCE.convert(req);
        boolean save = this.carModelService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('car:model:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarModelResp> findById(@PathVariable(value = "id") Long id){
        CarModel db = this.carModelService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarModelResp convert = CarModelConvert.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @Log(title = "汽车模型",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('car:model:edit')")
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarModelEditReq req){
        CarModel db = this.carModelService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarModel convert = CarModelConvert.INSTANCE.convert(req);

        boolean save = this.carModelService.updateById(convert);
        return save? R.ok(): R.fail();
    }

}
